Imports System.Data
Imports System.Data.SqlClient
Imports Conexao_liion

Partial Class CadItem2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)

        If Not (Page.IsPostBack = True) Then

            btnPesquisar.Enabled = False

            FillDDL()

            If (Session("blBack") = True) Then

                If Not (Session("strOperacao") = "I") Then

                    ddlHolding.SelectedValue = Session("strCodigoDaHolding").ToString
                    FillDDLCompanhia()
                    ddlCompanhia.SelectedValue = Session("strCodigoDaCia").ToString
                    FillDDLEstabelecimento()
                    ddlEstabelecimento.SelectedValue = Session("strCodigoDoEstabelecimento").ToString

                    CarregaGVW()

                    btnPesquisar.Enabled = True

                End If

            End If

        End If

        btnConsultar.Enabled = False
        btnAlterar.Enabled = False

    End Sub

    Private Sub FillDDL()

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodigoDaHolding, CodigoDaHolding + ' - ' + NomeDaHolding AS Holding" & _
                     " FROM Holding" & _
                     " ORDER BY CodigoDaHolding;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                ddlHolding.DataTextField = "Holding"
                ddlHolding.DataValueField = "CodigoDaHolding"
                ddlHolding.DataSource = mydatatable
                ddlHolding.DataBind()

                ddlHolding.Items.Insert(0, "")

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    Private Sub FillDDLCompanhia()

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodigoDaCia, CodigoDaCia + ' - ' + NomeDaCia AS Cia" & _
                 " FROM Companhia" & _
                 " WHERE CodigoDaHolding = '" & ddlHolding.SelectedValue & "'" & _
                 " ORDER BY CodigoDaCia;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                ddlCompanhia.DataTextField = "Cia"
                ddlCompanhia.DataValueField = "CodigoDaCia"
                ddlCompanhia.DataSource = mydatatable
                ddlCompanhia.DataBind()

                ddlCompanhia.Items.Insert(0, "")

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    Private Sub FillDDLEstabelecimento()

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodigoDoEstabelecimento, CodigoDoEstabelecimento + ' - ' + RazaoSocialDoEstabelecimento AS Estabelecimento" & _
                     " FROM Estabelecimento" & _
                     " WHERE CodigoDaHolding = '" & ddlHolding.SelectedValue & "'" & _
                     " AND CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'" & _
                     " ORDER BY CodigoDoEstabelecimento;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                ddlEstabelecimento.DataTextField = "Estabelecimento"
                ddlEstabelecimento.DataValueField = "CodigoDoEstabelecimento"
                ddlEstabelecimento.DataSource = mydatatable
                ddlEstabelecimento.DataBind()

                ddlEstabelecimento.Items.Insert(0, "")

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    Private Sub CarregaGVW()

        Dim strWhere As String = ""

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String = "SELECT A.CodigoDaCia, A.CodigoDoEstabelecimento," & _
                     " A.CodItem, A.DscItem, B.DscEspecieItem" & _
                     " FROM Item A, especieitem B"

            If Not (ddlHolding.Text = "") Then
                strWhere = " WHERE A.CodEspecieItem = B.CodEspecieItem AND A.CodigoDaHolding = '" & ddlHolding.SelectedValue & "'"
            End If

            If Not (ddlCompanhia.Text = "") Then
                strWhere = strWhere & " AND A.CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'"
            End If

            If Not (ddlEstabelecimento.Text = "") Then
                strWhere = strWhere & " AND A.CodigoDoEstabelecimento = '" & ddlEstabelecimento.SelectedValue & "'"
            End If

            If Not (Trim(txtCampoPesquisa.Text) = "") Then
                If (rbCodigo.Checked = True) Then
                    strWhere = strWhere & " AND A.CodItem LIKE '%" & txtCampoPesquisa.Text & "%'"
                Else
                    strWhere = strWhere & " AND A.DscItem LIKE '%" & txtCampoPesquisa.Text & "%'"
                End If
            End If

            If Not (strWhere = "") Then
                theQueryString = theQueryString & _
                         strWhere
            End If

            theQueryString = theQueryString & " ORDER BY CodigoDaCia, CodigoDoEstabelecimento, DscItem;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                gvwData.DataSource = mydatatable
                gvwData.DataBind()

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub



    Protected Sub ddlHolding_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlHolding.SelectedIndexChanged

        btnPesquisar.Enabled = False

        FillDDLCompanhia()
        FillDDLEstabelecimento()

    End Sub

    Protected Sub ddlCompanhia_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCompanhia.SelectedIndexChanged

        btnPesquisar.Enabled = True

        FillDDLEstabelecimento()

    End Sub

    Protected Sub rbCodigo_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbCodigo.CheckedChanged

        Try
            txtCampoPesquisa.Text = ""
            txtCampoPesquisa.MaxLength = 20
            txtCampoPesquisa.Width = 150

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub rbDescricao_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbDescricao.CheckedChanged

        Try
            txtCampoPesquisa.Text = ""
            txtCampoPesquisa.MaxLength = 40
            txtCampoPesquisa.Width = 330

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub btnPesquisar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPesquisar.Click
        CarregaGVW()
    End Sub

    Protected Sub gvwData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwData.PageIndexChanging

        Try
            CarregaGVW()

            gvwData.PageIndex = e.NewPageIndex
            gvwData.DataBind()

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub gvwData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwData.SelectedIndexChanged

        btnConsultar.Enabled = True
        btnAlterar.Enabled = True

    End Sub

    Protected Sub btnConsultar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConsultar.Click

        Session.Add("strOperacao", "C") ' Consultar

        Session.Add("strCodigoDaHolding", ddlHolding.SelectedValue)
        Session.Add("strNomeDaHolding", ddlHolding.Text)
        Session.Add("strCodigoDaCia", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodigoDoEstabelecimento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strCodItem", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))
        Session.Add("strDscItem", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(3).Text))

        Response.Redirect("CadItemData2.aspx")

    End Sub

    Protected Sub btnAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlterar.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodigoDaHolding", ddlHolding.SelectedValue)
        Session.Add("strCodigoDaCia", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodigoDoEstabelecimento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strCodItem", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))
        Session.Add("strDscItem", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(3).Text))

        Response.Redirect("CadItemData2.aspx")

    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click
        Response.Redirect("Default.aspx")
    End Sub

End Class
